package indi.kider.sys.gateway.authc;

import com.google.gson.Gson;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.ZuulFilterResult;
import com.netflix.zuul.context.RequestContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import pers.vankid.common.constant.HttpCode;
import pers.vankid.common.constant.HttpMethod;
import pers.vankid.common.model.base.CommonResponse;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by shixj on 2018/4/20.
 */
@Component
@Slf4j
public class AuthcFilter extends ZuulFilter {
    public AuthcFilter() {
        super();
    }

    @Override
    public boolean isStaticFilter() {
        return super.isStaticFilter();
    }

    @Override
    public String disablePropertyName() {
        return super.disablePropertyName();
    }

    @Override
    public boolean isFilterDisabled() {
        return super.isFilterDisabled();
    }

    @Override
    public ZuulFilterResult runFilter() {
        return super.runFilter();
    }

    @Override
    public int compareTo(ZuulFilter filter) {
        return super.compareTo(filter);
    }

    @Override
    public String filterType() {
        return "pre";//前置过滤
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Autowired
    private AuthcManager authcManager;

    @Override
    public Object run() {
        RequestContext     ctx     = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        log.info(String.format("%s >>> %s", request.getMethod(), request.getRequestURL().toString()));
        try {
            if (filter(request))
                return null;
            else if (authcManager.isLogined(request))
                authcManager.updateSginTime(request);//每次有效访问更新时间戳
            else {
                HttpServletResponse response = ctx.getResponse();
                response.setStatus(200);
                response.setCharacterEncoding("UTF-8");
                response.setContentType("application/json; charset=utf-8");
                ctx.setSendZuulResponse(false);
                response.getWriter().write(new Gson().toJson(CommonResponse.CommonResponseFactory(HttpCode.ERROR_UNAUTHORIZED, "请先登录系统！", "")));
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Value("${authc.none}")
    private boolean none;

    private boolean filter(HttpServletRequest request) {
        if (none)
            return none;
        return (request.getRequestURI().equals("/login") && request.getMethod().toUpperCase().equals("GET"))
                || request.getRequestURI().contains("app-client")
                || request.getRequestURI().contains("app-landlord")
                || request.getRequestURI().contains("qiniu")
                || request.getRequestURI().contains("applet")
                || request.getRequestURI().contains("balance-bill/bill/payState")
                || request.getRequestURI().contains("balance-bill/meterApi")
                || request.getRequestURI().contains("balance-bill/produce/canRefund");
    }


}
